package cn.liufuwen.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;

import cn.liufuwen.domain.User;
import cn.liufuwen.entity.UserDTO;
import cn.liufuwen.mapper.UserMapper;
import cn.liufuwen.service.UserService;
import lombok.extern.java.Log;
@Log
//注册为 Dubbo 服务
@Service(version = "${demo.service.version}")  
public class UserServiceImpl implements UserService {
	@Autowired
    private UserMapper userMapper;	
	@Override
	public int login(UserDTO user) {
		log.info("用户尝试登录"+user.toString());
		int r = 0;
		User userDO = new User();
		userDO.setUsername(user.getUsername());
		List<User> listUser = userMapper.selectByUsername(userDO);
		if(listUser.size() == 1){
			userDO = listUser.get(0);
			String passwordDTO = user.getPassword();
			String passwordDO = userDO.getPassword();
			if(passwordDTO.equals(passwordDO))
				r = userDO.getId();
		}
		return r;
	}

	@Override
	public int register(UserDTO user) {
		int r = 0;
		User userDO = new User();
		userDO.setUsername(user.getUsername());
		List<User> listUser = userMapper.selectByUsername(userDO);
		if(listUser.size() == 0){
			userDO.setPassword(user.getPassword());
			r = userMapper.insertSelective(userDO);
		}
		return r;
	}

}
